package src.com.atguigu06.project.team.view;

import src.com.atguigu06.project.team.domain.Employee;
import src.com.atguigu06.project.team.domain.Programmer;
import src.com.atguigu06.project.team.service.NameListService;
import src.com.atguigu06.project.team.service.TeamException;
import src.com.atguigu06.project.team.service.TeamService;

/**
 * ClassName: TeamView
 * Package: src.com.atguigu06.project.team.service
 * Description:
 *
 * @Author BuTang
 * @Create 2023/5/11 15:15
 * @Version 1.0
 */
public class TeamView {
    private NameListService listSvc = new NameListService();
    private TeamService teamSvc = new TeamService();

    public void enterMainMenu() {
        boolean loopFlag = true;
        char key = 0;

        do {
            if (key != '1') {
                listAllEmplyees();
            }
            System.out.print("1-团队列表  2-添加团队成员  3-删除团队成员 4-退出   请选择(1-4)：");
            key = TSUtility.readMenuSelection();
            System.out.println();
            switch (key) {
                case '1':
                    getTeam();
                    break;
                case '2':
                    addMember();
                    break;
                case '3':
                    deleteMember();
                    break;
                case '4':
                    System.out.print("确认是否退出(Y/N)：");
                    char yn = TSUtility.readConfirmSelection();
                    if (yn == 'Y')
                        loopFlag = false;
                    break;
            }
        } while (loopFlag);
    }



    public  void listAllEmplyees() {
        System.out.println("\n-------------------------------员工列表--------------------------------");
        Employee[] emp = listSvc.getAllEmployees();
        if (emp.length == 0) {
            System.out.println("没有员工");
        } else {
            System.out.println("ID\t姓名\t\t年龄\t工资\t\t职位\t\t状态\t\t奖金\t\t股票\t\t领用设备");
            for (int i = 0; i < emp.length; i++) {
                System.out.println("" + emp[i]);
            }
        }
    }


    private void getTeam() {
        System.out.println("\n-------------------------------团队列表--------------------------------");
        Programmer[] team =  teamSvc.getTeam();
        if (team.length == 0) {
            System.out.println("没有团队成员");
        } else {
            System.out.println("TID/Id\t姓名\t\t年龄\t工资\t\t职位\t\t状态\t\t奖金\t\t股票\t\t领用设备");
            for (int i = 0; i < team.length; i++) {
                System.out.println(""+team[i].getDetailsForTeam() );
            }
        }

    }

    ;

    private void addMember() {
        System.out.println("请输入你的目标");
        int id = TSUtility.readInt();
        try {
            Employee p =  listSvc.getEmployees(id);
            teamSvc.addMember(p);
            System.out.println("添加成功");
        }catch (TeamException e){
            System.out.println("添加失败：原因"+e.getMessage());
        }

    }

    private void deleteMember() {
        System.out.println("请输入你想删除的对象");
        int id = TSUtility.readInt();
        try {
            teamSvc.deleteMember(id);
            System.out.println("删除成功");
        } catch (TeamException e) {
            System.out.println("删除失败，原因："+e.getMessage());
        }
    }


    public static void main(String[] args) {
        TeamView a = new TeamView();
        a.enterMainMenu();
    }
}
